 jQuery(document).ready(function($){ 
    // var session = function getSession() {
//     $.ajax({
//         url:"/getSession.do",
//         dataType:"json",
//         success:function (data) {
//             if (data != "" && data != null){
//                 return data.session;
//             }
//             return "";
//         },
//         error:function () {
//             return "";
//         }
//     });
// }

    var session = "sss";
//     var session = "";
   

  
    /*登录注册*/
     var $form_modal = $('.cd-user-modal'), 
        $form_login = $form_modal.find('#cd-login'), 
        $form_signup = $form_modal.find('#cd-signup'), 
        $form_modal_tab = $('.cd-switcher'), 
        $tab_login = $form_modal_tab.children('li').eq(0).children('a'), 
        $tab_signup = $form_modal_tab.children('li').eq(1).children('a');
        // $main_nav = $('.video-head-photo'); 

     //获取页面传来的参数
    var id=getUrlParam('id');
    var videoNum=getUrlParam('videoNum');
    //获取课程视频参数
    var url="http://192.168.15.106/videl/search?id="+id;
    getVideoOfCourseData(url,id,videoNum,session);
    //绑定数据
     //var data='{"courseId":1,"courseName":"1213","videl":[{"id":1,"name":"第一节HoleWorld","url":"video/2.mp4 ","sid":1},{"id":3,"name":"asda","url":"video/3.mp4","sid":1}]}';
     //bindDetail(data,id,videoNum,session);
     //var data= '{"course":{"courseId":1,"courseName":"JavaEE",  "video":[{ "id":1 ,"videoName":"第一节HoleWorld", "videoUrl":"video/2.mp4"},{ "id":2 ,"videoName":"第二节环境搭建", "videoUrl":"video/3.mp4"},{ "id":2 ,"videoName":"第二节环境搭建", "videoUrl":"video/3.mp4"},{ "id":2 ,"videoName":"第二节环境搭建", "videoUrl":"video/3.mp4"},{ "id":2 ,"videoName":"第二节环境搭建", "videoUrl":"video/3.mp4"}]}}';
    
 
    //弹出窗口 
    if (session === "") {

            //$main_nav.children('ul').removeClass('is-visible');
            //show modal layer 
            $form_modal.addClass('is-visible');     
            //show the selected form 
            login_selected(); 
        //     $(".danmu-video").addClass("hide");
        //     $(".danmu-player-ctrl").addClass("hide");
        //     $("#logoImg").attr({title:"未登录"});
           
        // }else{
        //      $(".danmu-video").removeClass("hide");
        //      $(".danmu-player-ctrl").removeClass("hide");
        //      $("#logoImg").attr({title:session});
        }
 
    
 
    //关闭弹出窗口 
    $('.cd-user-modal').on('click', function(event){ 
        if( $(event.target).is($form_modal) || $(event.target).is('.cd-close-form') ) { 
            $form_modal.removeClass('is-visible'); 
        }     
    }); 
    //使用Esc键关闭弹出窗口 
    $(document).keyup(function(event){ 
        if(event.which=='27'){ 
            $form_modal.removeClass('is-visible'); 
        } 
    }); 
 
    //切换表单 
    $form_modal_tab.on('click', function(event) {
        event.preventDefault(); 
        ( $(event.target).is( $tab_login ) ) ? login_selected() : signup_selected(); 
    }); 
 

    function login_selected(){ 
        $form_login.addClass('is-selected'); 
        $form_signup.removeClass('is-selected'); 
        //$form_forgot_password.removeClass('is-selected'); 
        $tab_login.addClass('selected'); 
        $tab_signup.removeClass('selected'); 
    } 
 
    function signup_selected(){ 
        $form_login.removeClass('is-selected'); 
        $form_signup.addClass('is-selected'); 
        //$form_forgot_password.removeClass('is-selected'); 
        $tab_login.removeClass('selected'); 
        $tab_signup.addClass('selected'); 
    } 

    /*$("#register-button").click(function () {
        var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
        var email = $("#signup-email").val();
        var ps = $("#signup-password").val();
        var rps = $("#signup-password2").val();
        if (reg.test(email)){
            if (ps === rps){
                return true;
            }else{
                alert("两次输入的密码不一致");
            }
        }else{
            alert("邮箱格式错误");
        }
        return false;
    });*/

    $(".nv-goback a").click(function(){
        $(this).attr("href","../detail.html?id="+id);
    });
    pingfen();
    //pingfenHover();
    scoreFun($("#start"),{
           fen_d:22,//每一个a的宽度
           ScoreGrade:5//a的个数 10或者
         });

});


//使用AJAX获取数据,返回json数据
function getVideoOfCourseData(url,id,videoNum,session) {
    $.ajax({
        url:url,
        dataType:"json",
        success:function (data) {

           bindDetail(data,id,videoNum,session);

        },
        error:function () {
            return null;
        }
    })
}

//绑定数据
function bindDetail(data,id,videoNum,session) {

    /*{
    "Course": {
        "courseId": 1, 
        "courseName": "JavaEE", 
        "video": [
            {
                "id": 1, 
                "videoName": "第一节HoleWorld", 
                "videoUrl": "http://1.mp4"
            }, 
            {
                "id": 2, 
                "videoName": "第二节环境搭建", 
                "videoUrl": "http://2.mp4"
            }
        ]
    }
}*/
   //{"courseId":1,"courseName":"1213","videl":[{"id":1,"name":"第一节HoleWorld","url":"http://1.mp4 ","sid":1},{"id":3,"name":"asda","url":"http://baidu.com","sid":1}]}
    if (data!=null) {
        //data = JSON.parse(data);

        // var course=data.course;
        // var courseId=course.courseId;
        // var courseName=course.courseName;
        var courseId=data.courseId;
        var courseName=data.courseName;
        $(".nv-menu").text(courseName);
        $(".vcourse-group h2").text(courseName);
        var video=data.videl;
        for (var i=0; i <video.length; i++) {
            var videoId=video[i].id;
            // var videoName=video[i].videoName;
            // var videoUrl=video[i].videoUrl;
            var videoName=video[i].name;
            var videoUrl=video[i].url;
            var videoHref="file:///E:/workplace/guomai/mywork/bf2/bf/video.html?id="+id+"&videoNum="+i;
            if (videoNum==i) {
             $(".nv-follow").text(videoName);
             $(".vcourse-group").append('<div class="vcourse-bars-tit learning"><a href="#" title="'+videoName+'"> <h3 class="name overf">'+videoName+'</h3><div class="vcourse-bars-learning">学习中</div> </a> </div>');
             $("#danmup").DanmuPlayer({
                src : videoUrl,
                height : "480px", //区域的高度
                width : "100%", //区域的宽度
                
                // urlToGetDanmu : "query.php",
                // urlToPostDanmu : "stone.php"
              });
            }else{
             $(".vcourse-group").append('<div class="vcourse-bars-tit"><a href="'+videoHref+'" title="'+videoName+'"> <h3 class="name overf">'+videoName+'</h3> </a> </div>');
            }
        }

         
    }

    $("#video-r").css("height",$("#video-l").height());
    if (session === "") {

            $(".danmu-video").addClass("hide");
            $(".danmu-player-ctrl").addClass("hide");
            $("#logoImg").attr({title:"未登录"});
           
        }else{
             $(".danmu-video").removeClass("hide");
             $(".danmu-player-ctrl").removeClass("hide");
             $("#logoImg").attr({title:session});
        }
 
}
function pingfen(){
    if ($(".fenshu").val()=="") {
        $(".fenshu").text("未评");
    }
    $(".star_score").one("click",function(){
         alert("评价成功！");
         //将分数上传到服务器
         alert($(".fenshu").text()+"fahoa");
         $(".star_score a").attr('disabled',true);
    });
}

/*function pingfenHover(){
    $("#start").mouseover(function(){
        $("#start p").html('您的评分：<span class="fenshu"></span> 分');

    
}*/

//获取URL参数值的
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

function scoreFun(object,opts){
    var defaults={fen_d:16,ScoreGrade:10,types:["不满意","一般","满意","非常满意"],nameScore:"fenshu",parent:"star_score",attitude:"attitude"};options=$.extend({},defaults,opts);var
countScore=object.find("."+options.nameScore);var
startParent=object.find("."+options.parent);var
atti=object.find("."+options.attitude);var now_cli;var fen_cli;var
atu;var fen_d=options.fen_d;var
len=options.ScoreGrade;startParent.width(fen_d*len);var
preA=(5/len);for(var i=0;i
<len ;i++){var newSpan=$(
    "<a href='javascript:void(0)'></a>");newSpan.css({"left":0,"width":fen_d*(i+1),"z-index":len-i});newSpan.appendTo(startParent)}startParent.find("a").each(function(index,element){$(this).click(function(){
        now_cli=index;show(index,$(this))});$(this).mouseenter(function(){show(index,$(this))});$(this).mouseleave(function(){
        if(now_cli>=0){var scor=preA*(parseInt(now_cli)+1);startParent.find("a").removeClass("clibg");startParent.find("a").eq(now_cli).addClass("clibg");var
ww=fen_d*(parseInt(now_cli)+1);startParent.find("a").eq(now_cli).css({"width":ww,"left":"0"});if(countScore){
    countScore.text(scor);
   

}}else{startParent.find("a").removeClass("clibg");if(countScore){countScore.text("未评")}}})});function
show(num,obj){var n=parseInt(num)+1;var lefta=num*fen_d;var
ww=fen_d*n;var
scor=preA*n;atu=options.types[parseInt(num)];object.find("a").removeClass("clibg");obj.addClass("clibg");obj.css({"width":ww,"left":"0"});countScore.text(scor);atti.text(atu)}};